﻿using HHG.Common;
using OnLineExam.BLL;
using OnLineExam.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace OnLineExam.Web.Service
{
	public class PaperHelper
	{ 

		public static PaperViewModel GetPaperViewModel(int id) {

			var re = new PaperViewModel();
			var paperBll = new OnLineExam.BLL.PaperManager();
			var paperModel = paperBll.GetModel(id);

			re.PaperModel = paperModel;

			var probleminfo = paperBll.GetModel(id).ProblemInfo.Split(',');

			int single = int.Parse(probleminfo[0].Split(':')[1]);
			int multi = int.Parse(probleminfo[1].Split(':')[1]);
			int judge = int.Parse(probleminfo[2].Split(':')[1]);
			int fillblank = int.Parse(probleminfo[3].Split(':')[1]);
			int question = int.Parse(probleminfo[4].Split(':')[1]);
		 
			//单选:1:1,多选:1:1,判断:1:1,填空:1:1,问答:1:1

			//所有单选题;
			 
			//单选题
			var spInfo = new SpPaperInfoByString(probleminfo[0]);
			if (spInfo != null)
			{ 
				var dataSet = new OnLineExam.BLL.SingleProblem().GetList(spInfo.Count,"depid="+paperModel.DepID,"ID");
				 

				var items = new OnLineExam.BLL.SingleProblem().DataTableToList(dataSet.Tables[0]);
				re.SingleProblemModels = items;
				foreach (var item in items)
				{
					item.Mark = spInfo.Mark;
				}
			}

			//多选题
			  spInfo = new SpPaperInfoByString(probleminfo[1]);
			if (spInfo != null)
			{
				var dataSet = new OnLineExam.BLL.MultiProblem().GetList(spInfo.Count, "depid=" + paperModel.DepID, "ID");
				 
				var items = new OnLineExam.BLL.MultiProblem().DataTableToList(dataSet.Tables[0]);
				re.MultiProblemModels = items;
				foreach (var item in items)
				{
					item.Mark = spInfo.Mark;
				}
			}

			//判断题
			spInfo = new SpPaperInfoByString(probleminfo[2]);
			if (spInfo != null)
			{
				var dataSet = new OnLineExam.BLL.JudgeProblem().GetList(spInfo.Count, "depid=" + paperModel.DepID, "ID");
				

				var items = new OnLineExam.BLL.JudgeProblem().DataTableToList(dataSet.Tables[0]);
				re.JudgeProblemModels = items;
				foreach (var item in items)
				{
					item.Mark = spInfo.Mark;
				}
			}
			//填空题
			spInfo = new SpPaperInfoByString(probleminfo[3]);
			if (spInfo != null)
			{
				var dataSet = new OnLineExam.BLL.FillBlankProblem().GetList(spInfo.Count, "depid=" + paperModel.DepID, "ID");
				var items= new OnLineExam.BLL.FillBlankProblem().DataTableToList(dataSet.Tables[0]);
				re.FillBlankProblemModels = items;
				foreach (var item in items)
				{
					item.Mark = spInfo.Mark;
				}
			}

			//问答题
			spInfo = new SpPaperInfoByString(probleminfo[4]);
			if (spInfo != null)
			{
				var dataSet = new OnLineExam.BLL.QuestionProblem().GetList(spInfo.Count, "depid=" + paperModel.DepID, "ID");
			 
				var items = new OnLineExam.BLL.QuestionProblem().DataTableToList(dataSet.Tables[0]);
				re.QuestionProblemModels = items;
				foreach (var item in items)
				{
					item.Mark = spInfo.Mark;
				}
			}

			return re;
		}


		private class SpPaperInfoByString
		{

			/// <summary>
			/// 题目的数量
			/// </summary>
			public int Count { get; set; }

			/// <summary>
			/// 每题分数
			/// </summary>
			public int Mark { get; set; }
			public SpPaperInfoByString(string str)
			{
				try
				{
					this.Count = str.Split(':')[1].GetInt();
					this.Mark = str.Split(':')[2].GetInt();
				}
				catch (Exception ex)
				{
					throw ex;
				}

			}
		}
	}
}